package com.chucang.shucang.auth.support.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.LogoutSuccessEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.stereotype.Component;

/**
 * @author flitsneak
 * @email flitsneak@gmail.com
 * @date 2022/9/15 20:40
 * @description 退出处理
 */
@Slf4j
@Component
public class ShuCangLogoutSuccessEventHandler implements ApplicationListener<LogoutSuccessEvent> {
    @Override
    public void onApplicationEvent(LogoutSuccessEvent event) {
        Authentication authentication = (Authentication) event.getSource();
        if (authentication instanceof PreAuthenticatedAuthenticationToken) {
            handle(authentication);
        }
    }

    /**
     * 成功退出方法
     *
     * @param authentication a
     */
    public void handle(Authentication authentication) {
        log.info("用户：{} 退出成功", authentication.getPrincipal());
        //TODO 记录日志
        //SpringUtil.publishEvent();
    }
}
